﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using SapB1WebPOS.Utils;
using SapB1WebPOS.DAL;
using System.IO;
using System.Text;

namespace SapB1WebPOS.BLL
{
    public class SalesOrderBLL
    {
        public DataSet GetDocuments(string companyDB)
        {
            try
            {
                SalesOrderDAO connect = new SalesOrderDAO();
                DataSet dt = new DataSet(TableNameDefined.SALES_ORDER_HEADER);
                dt = connect.GetORDR(companyDB);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentByDocEntry(string companyDB, string docEntry)
        {
            try
            {
                DataSet dt = new DataSet(TableNameDefined.SALES_ORDER_HEADER);
                SalesOrderDAO connect = new SalesOrderDAO();
                dt = connect.GetORDRByDocEntry(companyDB, docEntry);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentForSearch(string companyDB, string keySearch)
        {
            try
            {
                DataSet dt = new DataSet(TableNameDefined.SALES_ORDER_HEADER);
                SalesOrderDAO connect = new SalesOrderDAO();
                dt = connect.GetORDRForSearch(companyDB, keySearch);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentLastRecord(string companyDB)
        {
            try
            {
                DataSet dt = new DataSet(TableNameDefined.SALES_ORDER_HEADER);
                SalesOrderDAO connect = new SalesOrderDAO();
                dt = connect.GetORDRLastRecord(companyDB);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public DataSet GetDocumentLineByDocEntry(string companyDB, string docEntry)
        {
            try
            {
                DataSet dt = new DataSet(TableNameDefined.SALES_ORDER_LINE);
                SalesOrderDAO connect = new SalesOrderDAO();
                List<object> lstParamValue = new List<object>();
                lstParamValue.Add(docEntry);
                dt = connect.GetRDR1ByDocEntry(companyDB, docEntry);
                return dt;
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return null;
            }
        }
        public string CreateHTML(DataSet dsData, string companyDB, string loginUser, string companyName, string pathHtmlfile)
        {
            try
            {
                double totalQuantity = 0, billTotal = 0, tax = 0, netValue = 0, discount = 0;
                string html = string.Empty;
                SAPUtils sapUtils = new SAPUtils(companyDB);
                using (StreamReader reader = new StreamReader(pathHtmlfile))
                {
                    html = reader.ReadToEnd();
                }
                if (html.Length > 0)
                {
                    // Header
                    html = html.Replace("$CompanyName", companyName);
                    html = html.Replace("$DocEntry", dsData.Tables[0].Rows[0]["DocEntry"].ToString());
                    html = html.Replace("$DocDate", DateTime.Parse(dsData.Tables[0].Rows[0]["DocDate"].ToString()).ToString("MM/dd/yyyy"));
                    html = html.Replace("$CustomerCode", dsData.Tables[0].Rows[0]["CardCode"].ToString());
                    html = html.Replace("$CustomerName", dsData.Tables[0].Rows[0]["CardName"].ToString());

                    StringBuilder itemList = new StringBuilder();

                    string whsName = string.Empty;
                    // Item List
                    itemList = new StringBuilder();
                    int i = 1;
                    foreach (DataRow r in dsData.Tables[1].Rows)
                    {

                        itemList.Append(" <tr>");
                        itemList.Append(" <td  align=\"center\">");
                        itemList.Append(i.ToString());
                        itemList.Append(" </td>");
                        itemList.Append(" <td   align=\"left\">");
                        itemList.Append(r["Dscription"].ToString());
                        itemList.Append(" </td>");
                        itemList.Append(" <td  align=\"right\">");
                        itemList.Append(sapUtils.FormatNumeric(r["Quantity"].ToString(), SAPUtils.QTYDEC));
                        itemList.Append(" </td>");
                        itemList.Append(" <td  align=\"center\">");
                        itemList.Append(r["unitMsr"].ToString());
                        itemList.Append(" </td>");
                        itemList.Append(" <td  align=\"right\">");
                        itemList.Append(sapUtils.FormatNumeric(r["PriceBefDi"].ToString(), SAPUtils.PRICEDEC));
                        itemList.Append(" </td>");
                        itemList.Append(" <td  align=\"right\">");
                        itemList.Append(sapUtils.FormatNumeric(r["VatPrcnt"].ToString(), SAPUtils.PERCENTDEC));
                        itemList.Append(" </td>");
                        itemList.Append(" <td  align=\"right\">");
                        itemList.Append(sapUtils.FormatNumeric(r["LineTotal"].ToString(), SAPUtils.PRICEDEC));
                        itemList.Append(" </td>");
                        itemList.Append(" </tr>");

                        itemList.Append(" <tr>");
                        itemList.Append(" <td  align=\"center\">");
                        itemList.Append(" </td>");
                        itemList.Append(" <td   align=\"left\">");
                        itemList.Append("Item Code:");
                        itemList.Append(" </td>");
                        itemList.Append(" <td  align=\"left\" colspan=\"2\">");
                        itemList.Append(r["ItemCode"].ToString());
                        itemList.Append(" </td>");
                        itemList.Append(" </tr>");

                        itemList.Append(" <tr>");
                        itemList.Append(" <td  align=\"center\" colspan=\"7\" style=\"height:5px;\">");
                        itemList.Append(" </td>");
                        itemList.Append(" </tr>");

                        billTotal += double.Parse(r["LineTotal"].ToString());
                        tax += double.Parse(r["VatSum"].ToString());

                        double disCountAmt = (double.Parse(r["LineTotal"].ToString()) * double.Parse(r["DiscPrcnt"].ToString())) / 100;

                        totalQuantity += double.Parse(r["Quantity"].ToString());
                        discount += disCountAmt;
                        whsName = r["WhsName"].ToString();
                        i++;

                    }
                    netValue = billTotal + tax;
                    html = html.Replace("$TRXList", itemList.ToString());

                    // Footer
                    html = html.Replace("$TotalQuantity", totalQuantity.ToString());
                    html = html.Replace("$BillTotal", sapUtils.FormatNumeric(billTotal.ToString(), SAPUtils.PRICEDEC));
                    html = html.Replace("$Tax", sapUtils.FormatNumeric(tax.ToString(), SAPUtils.PRICEDEC));
                    html = html.Replace("$NetValue", sapUtils.FormatNumeric(netValue.ToString(), SAPUtils.PRICEDEC));
                    html = html.Replace("$AmountText", Utils.Utils.NumberToText((long)netValue, ""));

                    return html;
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
                return "<b>Error: " + ex.Message + "</b>";
            }
            return "<b>Error: Can not read templete file.<b>";
        }
    }
}